package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.OrderSettingVO;
import com.itheima.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * 预约管理列表
     * @param year
     * @param month
     * @return
     */
    public List<OrderSettingVO> getOrderSettingByMonth(Integer year, Integer month) {
        String date = year+"-"+(month<10?"0"+month:month);
        List<OrderSettingVO> list = orderSettingMapper.getOrderSettingByMonth(date);
        return list;
    }

    /**
     * 设置预约人数
     * @param orderSetting
     */
    public void editNumberByDate(OrderSetting orderSetting) {
        List<OrderSetting> orderSettings = orderSettingMapper.getOrderSettingByDate(orderSetting.getOrderDate());
        if(orderSettings != null && orderSettings.size()>0){
            orderSettingMapper.update(orderSetting);
        }else {
            orderSettingMapper.insert(orderSetting.getOrderDate(),orderSetting.getNumber(),orderSetting.getReservations());
        }

    }

    /**
     * 批量插入数据
     * @param data
     */
    public void add(List<OrderSetting> data) {
        for (OrderSetting datum : data) {
            List<OrderSetting> orderSettings = orderSettingMapper.getOrderSettingByDate(datum.getOrderDate());
            if(orderSettings != null && orderSettings.size()>0){
                orderSettingMapper.update(datum);
            }else {
                orderSettingMapper.insert(datum.getOrderDate(),datum.getNumber(),datum.getReservations());
            }
        }
    }
}
